<?php
	/**
	*Author 雷日锦 362606856@qq.com 
	*控制器自动生成
	*/
	if(!defined("ROOT_PATH")) exit("die Access ");
	class tejiaControl extends skymvc{
		
		public function __construct(){
			parent::__construct();
		}
		
		public function onDefault(){
			$where=" is_temp=0 AND status<10 AND siteid=".SITEID;
			$url=APPADMIN."?m=tejia&a=default";
			$limit=20;
			$start=get("per_page","i");
			
			$option=array(
				"start"=>intval(get_post('per_page')),
				"limit"=>$limit,
				"order"=>" id DESC",
				"where"=>$where
			);
			$rscount=true;
			$data=M("tejia")->select($option,$rscount);
			if($data){
				foreach($data as $v){
					$shopids[]=$v['shopid'];
				}
				$shops=M("shop")->getShopByIds($shopids);
				$type=M("tejia")->type_list();
				foreach($data as $k=>$v){
					$v['shop_name']=$shops[$v['shopid']]['shopname'];
					$v['type_name']=$type[$v['typeid']];			
					$data[$k]=$v;
				}
				
			}
			 
			$pagelist=$this->pagelist($rscount,$limit,$url);
			$this->smarty->assign(
				array(
					"data"=>$data,
					"pagelist"=>$pagelist,
					"rscount"=>$rscount,
					"url"=>$url
				)
			);
			$this->smarty->display("tejia/index.html");
		}
		
		public function onAdd(){
			$id=get_post("id","i");
			if($id){
				$row=M("tejia")->selectRow(array("where"=>"id={$id}"));
				
			}
			if(empty($row)){
				$row=M("tejia")->selectRow("shopid=0 AND tablename='system' AND siteid=".SITEID." AND is_temp=1");
				if(empty($row)){
					M("tejia")->insert(array(
						"dateline"=>time(),
						"shopid"=>0,
						"is_temp"=>1,
						"siteid"=>SITEID,
						"tablename"=>"system"
					));
					$row=M("tejia")->selectRow("shopid=0 AND siteid=".SITEID." AND is_temp=1");
				}
			}
			$this->smarty->assign(array(
				"data"=>$row
			));
			$this->smarty->display("tejia/add.html");
		}
		
		public function onSave(){
			$id=get_post("id","i");
			$starttime=strtotime(post('starttime'));
			$endtime=strtotime(post('endtime'));
			$data=M("tejia")->postData();
			$data['starttime']=$starttime;
			$data['endtime']=$endtime;
			$data['siteid']=SITEID;
			if($id){
				$data['is_temp']=0;
				M("tejia")->update($data,"id='$id'");
			}else{
				M("tejia")->insert($data);
			}
			$this->goall("保存成功");
		}
		
		public function onStatus(){
			$id=get_post('id',"i");
			$status=get_post("status","i");
			M("tejia")->update(array("status"=>$status),"id=$id");
			$this->goall("状态修改成功",0);
		}
		
		public function onDelete(){
			$id=get_post('id',"i");
			M("tejia")->delete("id=".$id);
			$this->goall("删除成功",0);
		}
		
		
	}

?>